﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Evan.Template
{
    /// <summary>
    /// 
    /// </summary>
    public class Base
    {
        


        /// <summary>
        /// 获取某个栏目->某个类型 下的所有URL列表
        /// </summary>
        /// <param name="curRewrite">当前栏目-某个类型的伪静态规则 包含Page的默认为列表页</param>
        /// <param name="curChannel">当前栏目的实体</param>
        /// <param name="DefaultPage">默认页面 输入栏目目录后的默认页面</param>
        /// <returns></returns>
        public List<string> GetUrls(string curRewrite, dynamic curChannel, string DefaultPage)
        {
            List<string> Urls = new List<string>();
            string ChanneWebPath = curChannel.WebPath;
            string TableName = Evan.Template.TemplateHelper.GetTableNameByPageType(Convert.ToInt32(curChannel.PageType));

            if (!string.IsNullOrEmpty(TableName))
            {

                if (curRewrite.IndexOf('{') > -1 && curRewrite.IndexOf('}') > -1)
                {
                    //有参数的
                    //拿到所有参数列表
                    //伪静态中的所有参数 Page 单独判断  
                    //Regex regex = new Regex(@"{[^{}]*}", RegexOptions.IgnoreCase);
                    MatchCollection mc = Regex.Matches(curRewrite, "{(.*?)}");
                    List<string> ArgList = new List<string>();
                    string _temp = "";
                    foreach (Match item in mc)
                    {
                        ArgList.Add(item.Value.TrimStart('{').TrimEnd('}'));
                    }
                    //如果参数包含Page  那就是要生成列表页,Page这条伪静态中包含的其他参数都关联到当前栏目 - 强制规定
                    //根据栏目拿到表 , 有参数 , 根据表拿到对应的数据进行替换
                    if (ArgList.Contains("Page"))
                    {
                        //拿到当前表
                        //拿列表页
                        //列表页生成两条URL index.html  第一页的URL
                        Evan.Model.SysModel.PageModels PageList = new Evan.DataBase.DbOperat(TableName).GetPage_2(Convert.ToInt32(curChannel.PageSize), 1, string.Format("States=1 and ClassId={0}", Convert.ToInt32(curChannel.Id)), Evan.Template.TemplateHelper.GetSqlOrderStr(curChannel));
                        if (PageList.AllCount > 0)
                        {
                            //生成index.html
                            Urls.Add(ChanneWebPath + DefaultPage);
                            //按照分页拿到URL
                            for (int i = 1; i < PageList.PageCount; i++)
                            {
                                _temp = "";
                                //替换参数 //每一条
                                foreach (string item in ArgList)
                                {
                                    if (item == "Page")
                                    {
                                        _temp += curRewrite.Replace("{" + item + "}".ToString(), i.ToString());
                                    }
                                    else
                                    {
                                        _temp += curRewrite.Replace("{" + item + "}".ToString(), Evan.Model.ConvertModel.ConvertToDictionary(curChannel)[item].ToString());
                                    }
                                }
                                //替换完成后加入列表
                                Urls.Add(ChanneWebPath + _temp);
                            }
                        }
                    }
                    else
                    {
                        //拿其他页面 没有分页  直接遍历数据
                        List<dynamic> Lists = new Evan.DataBase.DbOperat(TableName).GetList_2(0, string.Join(",", ArgList.ToArray()), string.Format("States=1 and ClassId={0}", Convert.ToInt32(curChannel.Id)), Evan.Template.TemplateHelper.GetSqlOrderStr(curChannel), null);
                        if (Lists.Count > 0)
                        {
                            //按照详细拿到URL
                            foreach (var item in Lists)
                            {
                                _temp = "";
                                //替换参数
                                foreach (string itemArg in ArgList)
                                {
                                    _temp += curRewrite.Replace("{" + itemArg + "}".ToString(), Evan.Model.ConvertModel.ConvertToDictionary(item)[itemArg].ToString());
                                }
                                Urls.Add(ChanneWebPath + _temp);
                            }
                        }
                    }
                }
                else
                {
                    //没有参数的  直接返回
                    Urls.Add(ChanneWebPath + curRewrite);
                }

            }
            else {
                //综合页没有表名字 , 在这里生成对应的链接
                if (Convert.ToInt32(curChannel.PageType) == 0 && !string.IsNullOrEmpty(curRewrite))
                {
                    Urls.Add(ChanneWebPath + curRewrite);
                }
            }
            return Urls;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="curChannel"></param>
        /// <param name="UrlList"></param>
        /// <returns></returns>
        public List<string> GetRequestUrl_Default(dynamic curChannel,List<string> UrlList)
        {
            string RewriteDefault = curChannel.RewriteDefault;
            int TemplateDefault = Convert.ToInt32(curChannel.TemplateDefault);
            string ChanneWebPath = curChannel.WebPath;

            //if (TemplateDefault > 0 && !string.IsNullOrEmpty(RewriteDefault))
            //{
            //    List<string> l = GetUrls(RewriteDefault, curChannel, "Default.html");
            //    UrlList = UrlList.Concat(l).ToList<string>();
            //}
            //如果不为空就生成
            if (!string.IsNullOrEmpty(RewriteDefault))
            {
                //如果不是外链就生成
                if (!RewriteDefault.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !RewriteDefault.StartsWith("//", StringComparison.OrdinalIgnoreCase))
                {
                    List<string> l = GetUrls(RewriteDefault, curChannel, "Default.html");
                    UrlList = UrlList.Concat(l).ToList<string>();
                }
            }

            return UrlList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="curChannel"></param>
        /// <param name="UrlList"></param>
        /// <returns></returns>
        public List<string> GetRequestUrl_List(dynamic curChannel, List<string> UrlList)
        {
            string RewriteList = curChannel.RewriteList;
            int TemplateList = Convert.ToInt32(curChannel.TemplateList);
            string ChanneWebPath = curChannel.WebPath;

            if (TemplateList > 0 && !string.IsNullOrEmpty(RewriteList))
            {
                List<string> l = GetUrls(RewriteList, curChannel, "index.html");
                UrlList = UrlList.Concat(l).ToList<string>();
            }

            return UrlList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="curChannel"></param>
        /// <param name="UrlList"></param>
        /// <returns></returns>
        public List<string> GetRequestUrl_Detail(dynamic curChannel, List<string> UrlList)
        {
            string RewriteDetail = curChannel.RewriteDetail;
            int TemplateDetail = Convert.ToInt32(curChannel.TemplateDetail);
            string ChanneWebPath = curChannel.WebPath;

            if (TemplateDetail > 0 && !string.IsNullOrEmpty(RewriteDetail))
            {
                List<string> l = GetUrls(RewriteDetail, curChannel, "Detail.html");
                UrlList = UrlList.Concat(l).ToList<string>();
            }

            return UrlList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="curChannel"></param>
        /// <param name="UrlList"></param>
        /// <returns></returns>
        public List<string> GetRequestUrl_SinglePage(dynamic curChannel, List<string> UrlList)
        {
            string RewriteSinglePage = curChannel.RewriteSinglePage;
            int TemplateSinglePage = Convert.ToInt32(curChannel.TemplateSinglePage);
            string ChanneWebPath = curChannel.WebPath;

            if (TemplateSinglePage > 0 && !string.IsNullOrEmpty(RewriteSinglePage))
            {
                List<string> l = GetUrls(RewriteSinglePage, curChannel, "Detail.html");
                UrlList = UrlList.Concat(l).ToList<string>();
            }

            return UrlList;
        }

        /// <summary>
        /// 获取所有的栏目
        /// </summary>
        /// <returns></returns>
        public List<dynamic> GetAllChannel()
        {
            return new Evan.DataBase.DbOperat("Channel").GetList(" States=1 ", "SortId Asc,Id Asc");
        }

        /// <summary>
        /// 获取所有栏目的链接地址
        /// </summary>
        /// <param name="ChannelId">如果只是获取某一个栏目的话 传入栏目ID  默认是0(获取所所有栏目)</param>
        /// <returns></returns>
        public List<string> GetUrlList(int ChannelId = 0)
        {
            List<string> Urls = new List<string>();
            if (ChannelId > 0)
            {
                //获取某一个栏目下面的
                dynamic Channel = new Evan.DataBase.DbOperat("Channel").GetModel(ChannelId);
                Urls = GetRequestUrl_Default(Channel, Urls);
                Urls = GetRequestUrl_List(Channel, Urls);
                Urls = GetRequestUrl_Detail(Channel, Urls);
                Urls = GetRequestUrl_SinglePage(Channel, Urls);
            }
            else
            {
                //获取所有的
                //遍历栏目即可
                List<dynamic> AllChannel = GetAllChannel();
                foreach (var Channel in AllChannel)
                {
                    Urls = GetRequestUrl_Default(Channel, Urls);
                    Urls = GetRequestUrl_List(Channel, Urls);
                    Urls = GetRequestUrl_Detail(Channel, Urls);
                    Urls = GetRequestUrl_SinglePage(Channel, Urls);
                }
            }
            return Urls;
        }

    }
}
